package cn.ffcs.uec.common.component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Treeitem;

import cn.ffcs.uec.common.manager.UecBdPsnclManager;
import cn.ffcs.uec.common.vo.UecBdPsncl;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.zkplus.zul.tree.node.impl.TreeNodeImpl;
import cn.ffcs.uom.staffrole.constants.StaffRoleConstants;
import lombok.Getter;
import lombok.Setter;

public class BdPsnclTreeBandboxExt extends Bandbox implements IdSpace {

    private static final long serialVersionUID = 597212753811692609L;

    private final String zul = "/pages_nmg/uec_staff/comp/uec_bd_psncl_tree_bandbox_ext.zul";
    private UecBdPsnclManager uecBdPsnclManager = (UecBdPsnclManager) ApplicationContextUtil
			.getBean("uecBdPsnclManager");
    /**
     * 人员类别
     */
    private UecBdPsncl uecBdPsncl;

    /**
     * 人员类别树
     */
    private BdPsnclTree bdPsnclTree;

    @Getter
    @Setter
    private String mode;// 单选和多选两种模式

    @Getter
    @Setter
    private String range;// 只选择末级节点和任意选择节点两种模式

    @Getter
    private List<UecBdPsncl> uecBdPsncls;

    private List<UecBdPsncl> resultArr;// 选择节点列表,left=节点标识,right=节点中文名
    private  List<UecBdPsncl> listTemp1 = new ArrayList<UecBdPsncl>();//临时加的变量
    @Getter
    @Setter
    private String resultText;// 返回文本

    public BdPsnclTreeBandboxExt() {
        Executions.createComponents(zul, this, null);
        Components.wireVariables(this, this);
        Components.addForwards(this, this, '$');
        bdPsnclTree.setBdPsnclTreeBandboxExt(this);
    }

    /**
     * 初始化
     */
    @SuppressWarnings("unused")
    public void onCreate() {
        if ("multipleSelection".equals(mode)) {
            // 多选
            bdPsnclTree.setMultiple(true);
        } else {
            // 单选
            bdPsnclTree.setMultiple(false);
        }
        //显示勾选框
       // bdPsnclTree.setCheckmark(true);
        
        @SuppressWarnings("unchecked")
        Set<Treeitem> set = this.bdPsnclTree.getSelectedItems();
        // ..实现初始化加载已选信息
        // TODO 加了监听box就会多出下面的空白
        // 写死高度像素值暂时解决
        this.addEventListener(Events.ON_OPEN, new EventListener() {

            @SuppressWarnings({ "unchecked", "rawtypes" })
            @Override
            public void onEvent(Event event) throws Exception {
                // set initial check state
                Collection<Treeitem> tis = bdPsnclTree.getItems();
                for (Treeitem ti : tis) {
                    UecBdPsncl uecBdPsncl = (UecBdPsncl) ((TreeNodeImpl) ti.getValue()).getEntity();
                }
                if (resultArr != null) {
                    for (Treeitem ti : tis) {
                        UecBdPsncl uecBdPsncl = (UecBdPsncl) ((TreeNodeImpl) ti.getValue())
                                .getEntity();
                        if (resultArr.contains(uecBdPsncl)) {
                            ti.setSelected(true);
                        } else {
                            ti.setSelected(false);
                        }
                    }
                }
            }
        });
    }

    public void setUecBdPsncls(List<UecBdPsncl> uecBdPsncls) {
        String psnclName = "";
        if (uecBdPsncls != null && uecBdPsncls.size() > 0) {
            for (int i = 0; i < uecBdPsncls.size(); i++) {
                if (i == uecBdPsncls.size() - 1) {
                    psnclName += uecBdPsncls.get(i).getName();
                } else {
                    psnclName += uecBdPsncls.get(i).getName() + ",";
                }
            }
        }
        this.setValue(psnclName);
        this.uecBdPsncls = uecBdPsncls;
    }

    /**
     * 确认按钮
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void onClick$okButton() {
        Set<Treeitem> itemsSet = this.bdPsnclTree.getSelectedItems();
        List<UecBdPsncl> list = new ArrayList<UecBdPsncl>();
        if (itemsSet != null) {
            Iterator<Treeitem> it = itemsSet.iterator();
            while (it.hasNext()) {
                Treeitem ti = it.next();
                if(!"multipleSelection".equals(mode)) {
                	if (ti.getTreechildren() != null) {
                        // 不是末级节点
                        continue;
                    }
                	UecBdPsncl sl = (UecBdPsncl) ((TreeNodeImpl) ti.getValue()).getEntity();
                	listTemp1.add(sl);
                }else {
                //判断是否是末级节点
             /*   if (ti.getTreechildren() != null) {
                    // 不是末级节点
                    continue;
                }*/
               // getBoolean(ti.getChildren());
                UecBdPsncl sl = (UecBdPsncl) ((TreeNodeImpl) ti.getValue()).getEntity();
               // if (sl != null && !StaffRoleConstants.IS_PARENT.equals(sl.getParentId())) {
                if (sl != null ) {
                	List<UecBdPsncl> qryUecBdPsnclByPid = uecBdPsnclManager.qryUecBdPsnclByPid(sl);
                	if(qryUecBdPsnclByPid !=null && qryUecBdPsnclByPid.size() != 0) {
                		getCollectionList(qryUecBdPsnclByPid);
                	}else {
                		listTemp1.add(sl);
                	}
                  //  list.add(sl);
                }
            }
            }     
        }
      //  this.setUecBdPsncls(list);
        this.setUecBdPsncls(listTemp1);
        listTemp1=new ArrayList<UecBdPsncl>();
        this.close();
    }

    /**
     * 取消按钮
     */
    public void onClick$cancelButton() {
        this.close();
    }

    public List<UecBdPsncl> getResultArr() {
        return resultArr;
    }

    public UecBdPsncl getUecBdPsncl() {
        return this.uecBdPsncl;
    }

    public void setUecBdPsncl(UecBdPsncl uecBdPsncl) {
        this.setValue(uecBdPsncl == null ? "" : uecBdPsncl.getName());
        this.uecBdPsncl = uecBdPsncl;
        Events.postEvent("onGetBdPsncl", this, uecBdPsncl);
    }

    /**
     * 快速设置初始值
     * 
     * @author 曾臻
     * @date 2013-6-19
     * @param codeList
     *            编码列表
     */
    public void setInitialValue(List<UecBdPsncl> uecBdPsncls) {
        String str = "";
        resultArr = new ArrayList<UecBdPsncl>();
        if (uecBdPsncls != null && uecBdPsncls.size() > 0) {
            for (UecBdPsncl uecBdPsncl : uecBdPsncls) {
                str += uecBdPsncl.getName() + ",";
                resultArr.add(uecBdPsncl);
            }
        }
        resultText = StringUtils.removeEnd(str, ",");
        this.setText(resultText);
    }
    
    /**
     * 判断是否有子集，有的话继续遍历
     * 
     * @author 白美平
     * @date 2019-3-01
     * @param codeList
     *            编码列表
     */
   
    public void getCollectionList(List<UecBdPsncl> uecBdPsncls) {
    	if (uecBdPsncls != null && uecBdPsncls.size() > 0) {
    		UecBdPsncl uecBdPsncl=null;
    		for (int i=0;i<uecBdPsncls.size();i++) {
    			uecBdPsncl=uecBdPsncls.get(i);
    			List<UecBdPsncl> qryUecBdPsnclByPid = uecBdPsnclManager.qryUecBdPsnclByPid(uecBdPsncl);
    			if(qryUecBdPsnclByPid.size() >0 && qryUecBdPsnclByPid != null) {
    				getCollectionList(qryUecBdPsnclByPid);
    			}
    			else {
    				if(uecBdPsncl != null) {
    				   listTemp1.add(uecBdPsncl);
    				}
    			}
    		}
    	}
    	
    }
}
